package cn.shop.dys.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.support.v4.view.ViewCompat;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * 测试view
 * Created by jian on 2016/4/16.
 */
public class TestView extends View{

    private Paint mLeftPaint;
    private Paint mRightPaint;

    public TestView(Context context) {
        super(context);
        init();
    }

    private void init() {
        mLeftPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLeftPaint.setColor(Color.RED);
        mRightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRightPaint.setColor(Color.GREEN);
    }
    public void startAnim(){
        ValueAnimator valueAnimator = ObjectAnimator.ofFloat(1.0f);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                degree = animation.getAnimatedFraction()*(float) Math.PI;
                Log.d("test","degree"+degree);
                ViewCompat.postInvalidateOnAnimation(TestView.this);
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {

            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setDuration(3000);
        valueAnimator.start();
    }
    private PointF left=new PointF(50,100);
    private PointF center=new PointF(150,100);
    private float radius=50;
private float degree;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(left.x - radius, left.y - radius, left.x + radius, left.y + radius, mLeftPaint);
      //  Log.d("test","width"+(float)(center.x+radius*Math.cos(degree)));
        if(degree>=Math.PI*0.5f){
            Log.d("test","相等");
            canvas.drawRect((float) (center.x +3.0f*radius * Math.cos(degree)), center.y - radius,
                    (float) (center.x + radius * Math.cos(degree)), center.y + radius, mRightPaint);
        }else {
            canvas.drawRect((float)(center.x+radius*Math.cos(degree)),center.y-radius,
                    (float)(center.x+3.0f*radius*Math.cos(degree)),center.y+radius,mRightPaint);
        }
}
}
